package com.lzl.service;

import com.lzl.constant.CommonStatusEnum;
import com.lzl.dto.PriceRule;
import com.lzl.mapper.PriceRuleMapper;
import com.lzl.remote.ServiceMapClient;
import com.lzl.request.ForecastPriceDTO;
import com.lzl.responese.DirectionResponese;
import com.lzl.responese.ForecastPriceResponese;
import com.lzl.responese.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

/**
 * //TODO
 *
 * @author zhaolinlin
 * @description //TODO
 * @createTime 2022/8/18 20:37
 */
@Service
@Slf4j
public class ForecastPriceService {

    @Autowired
    ServiceMapClient serviceMapClient;

    @Autowired
    PriceRuleMapper priceRuleMapper;

    public ResponseResult forecastPrice(String depLongitude,String depLatitude,String destLongitude,String destLatitude){

        log.info("出发地经度：" + depLongitude);
        log.info("出发地纬度：" + depLatitude);
        log.info("目的地经度：" + destLongitude);
        log.info("目的地纬度：" + destLongitude);

        ForecastPriceDTO forecastPriceDTO = new ForecastPriceDTO();
        forecastPriceDTO.setDepLongitude(depLongitude);
        forecastPriceDTO.setDepLatitude(depLatitude);
        forecastPriceDTO.setDestLongitude(destLongitude);
        forecastPriceDTO.setDestLatitude(destLatitude);
        ResponseResult<DirectionResponese> directon = serviceMapClient.directon(forecastPriceDTO);
        Integer direction = directon.getData().getDirection();
        Integer duration = directon.getData().getDuration();
        log.info("路程 : " + direction + ";" + "费时 : " + duration);
        log.info("根据计价规则获取路程价格");
        HashMap<String, Object> map = new HashMap<>();
        map.put("city_code","110000");
        map.put("vehicle_type","1");
        List<PriceRule> priceRules = priceRuleMapper.selectByMap(map);
        if(priceRules.size() == 0){
          return ResponseResult.fail(CommonStatusEnum.PRICE_RULE_EMPTY.getCode(),CommonStatusEnum.PRICE_RULE_EMPTY.getValue());
        }
        PriceRule priceRule = priceRules.get(0);
        double price = getPrice(direction, duration, priceRule);
        ForecastPriceResponese forecastPriceResponese = new ForecastPriceResponese();
        forecastPriceResponese.setPrice(price);

        return ResponseResult.success(forecastPriceResponese);

    }

    public static double getPrice(Integer direction,Integer duration ,PriceRule priceRule){
        BigDecimal price = new BigDecimal(0);
        //计算路程费用
        Integer startMile = priceRule.getStartMile();
        BigDecimal startBigMile = new BigDecimal(direction);
        BigDecimal  kilometers= startBigMile.divide(new BigDecimal(1000), 2, BigDecimal.ROUND_HALF_UP);
        Double residueKilometers = kilometers.subtract(new BigDecimal(startMile)).doubleValue();
        double mile = residueKilometers < 0 ? 0 :  residueKilometers;
        BigDecimal unitPriceMile = new BigDecimal(priceRule.getUnitPricePerMile());
        BigDecimal sumMile = unitPriceMile.multiply(new BigDecimal(mile));
        price = price.add(sumMile);

        //计算计时费用
        Double startFare = priceRule.getStartFare();
        BigDecimal startBigFare = new BigDecimal(startFare);
        BigDecimal dur = new BigDecimal(duration);
        BigDecimal unitperMinute = new BigDecimal(priceRule.getUnitPricePerMinute());
        BigDecimal hour = dur.divide(new BigDecimal(60), 2, BigDecimal.ROUND_HALF_UP);
        BigDecimal multiply = hour.multiply(unitperMinute);
        price =price.add(startBigFare).add(multiply);
        //总费用
        double totalPrice = price.doubleValue();
        return totalPrice;

    }
//    public static void main(String[] args) {
//        PriceRule priceRule = new PriceRule();
//        priceRule.setUnitPricePerMile(1.8);
//        priceRule.setUnitPricePerMinute(0.5);
//        priceRule.setStartFare(10.0);
//        priceRule.setStartMile(3);
//        System.out.println(getPrice(6500,1800,priceRule));
//    }
}
